package cn.ven.controller;

import cn.ven.entity.ItemBook;
import cn.ven.entity.MyUser;
import cn.ven.entity.OrdInfo;
import cn.ven.entity.Order;
import cn.ven.service.OrderService;
import cn.ven.tool.RespJson;
import cn.ven.tool.Token;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;



@Controller
@RequestMapping("/api/adm/ord")
public class OrdAdmController {
    private Logger logger =LoggerFactory.getLogger(OrdAdmController.class);

    @Autowired
    OrderService orderService;

    //根据订单号添加快递单号
    @RequestMapping("/addShip")
    @ResponseBody
    public Map<String,Object> addShip(@RequestBody Order order){
        orderService.addShip(order);
        return RespJson.pack(200,"添加成功","");
    }

    //模糊查询查找对应订单详细信息以及用户一些信息，可分页
    @RequestMapping("/findOrdInfo/{start}/{num}")
    @ResponseBody
    public Map<String,Object> findOrdInfo(@PathVariable("start")Integer start,
                                          @PathVariable("num")Integer num,
                                          String account, String nickname,
                                          String phone, Integer ispay,
                                          Date tstart,Date tend){
        Map<String,Object> map =new HashMap<>();//用来模糊查询的集合
        Map<String,Object> info =new HashMap<>();//用来返回给前端的查询结果
        map.put("start",start);
        map.put("num",num);
        if (account!=null) map.put("account",account);  //当填用户名的时候
        if (nickname!=null) map.put("nickname",nickname);  //当有网名的时候
        if (phone!=null) map.put("phone",phone); //当有电话的时候
        if (ispay!=null) map.put("ispay",ispay);  //当ispay有写的时候
        if (tstart!=null) map.put("tstart",start); //当有填起始时间的时候
        if (tend!=null) map.put("tend",tend); //当有结尾时间的时候
        try { //主动处理一些可能会发生的异常
            List<OrdInfo> ordInfos = orderService.findOrdInfo(map);
            logger.info(String.valueOf(ordInfos));
            info.put("ordInfos",ordInfos);
            info.put("total",orderService.getCount(map));//总页数
        }catch (Exception e){
            logger.info(String.valueOf(e));
            return RespJson.pack(500,"查询数据有误",null);
        }
        return RespJson.pack(200,"",info);
    }



    //根据订单号获取该订单明细以及书本信息
    @RequestMapping("/findItemInfoByOrdid/{ordid}")
    @ResponseBody
    public Map<String,Object> findItemInfoByOrdid(@PathVariable("ordid")Integer ordid){
        logger.info("ordid==="+ordid);
        if (Token.getAccount()==null||Token.getAccount()instanceof MyUser ==false){ //当用户为null的时候，也就是没有登录,或者拿到的不是MyUser对象
            return RespJson.pack(401,"Token过期，请重新登录.....",null);
        }
        List<ItemBook> itemBooks = orderService.findItemInfoByOrdid(ordid);
        return RespJson.pack(200,"",itemBooks);
    }

}
